﻿using ConfExpert.Forms.Base;
using DevTeam;
using System;
using System.ComponentModel;
using System.Data;
using System.Web.UI.WebControls;

namespace ConfExpert.Forms.Planlama
{
    public partial class AtelyeDegisikligi : XForm
    {
        DataSet d;
        public AtelyeDegisikligi()
        {
            InitializeComponent();
        }

        private void AtelyeDegisikligi_Load(object sender, EventArgs e)
        {
            btnGetir.ItemClick += btnGetir_ItemClick;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            Rfrsh();
            gridView1.FormatAsDateTime("Tarih");
            gridView1.FormatAsNumber(0, "Adet");
            gridView1.AddSummary("Adet");
            gridView1.MakeReadOnly("AtelyeId");
            gridView1.MakeColumnInvisible("PlanlamaId");
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView1.ShowingEditor += gridView1_ShowingEditor;
            chkOnceki.CheckedChanged += chkOnceki_CheckedChanged;
            btnSend.ItemClick += BtnSend_ItemClick;
            gridView1.BestFitColumns();
        }

        private void BtnSend_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isProduction)
                ExecuteNonQuery("SendMailAtelyeDegisen", WhoAmI);
            btnSend.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        private void GetPending()
        {
            int pending = 0;
            if (d != null)
                pending = d.Tables[1].Rows[0]["Adet"].ToInt();

            if (pending > 0)
            {
                btnSend.Caption = pending.ToStr() + " Dosya İçin Bilgilendir";
                btnSend.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            }
            else
                btnSend.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        void btnGetir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Rfrsh();
        }

        void chkOnceki_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Rfrsh();
        }

        void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView gv = sender as GridView;
            DataRow dr = gridView1.GetFocusedDataRow();
            bool dateset = dr["Tarih"] != DBNull.Value;
            if (gridView1.FocusedColumn.FieldName == "AtelyeId")
                e.Cancel = dateset;
            else
                e.Cancel = true;
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            ExecuteNonQuery("UpdAtelyeDegistir", gridView1.SerializeChanges(), WhoAmI);
            Rfrsh();
        }

        private void Rfrsh()
        {
            d = ExecuteDataSet("GetAtelyeDegistir", chkOnceki.Checked, memoEdit1.Text.Replace(Environment.NewLine, ","), WhoAmI);
            gridView1.Init(d.Tables[0]);
            GetPending();

            gridView1.BestFitColumns();
        }
    }
}
